#include <iostream>
#include <vector>
#include <queue>
using namespace std;

class Solution {
public:
    long long pickGifts(vector<int>& g, int k) {
        priority_queue<int> q(g.begin(), g.end());
        while (k--)
        {
            int tmp = q.top();
            q.pop();
            q.push(sqrt(tmp));
        }
        long long sum = 0;
        while (k-- && !q.empty())
        {
            sum += q.top();
            q.pop();
        }
        return sum;
    }
};